// 导入express
const express = require('express')

let Token = require('./utils/jwtutil')


const serverApi = express()

// 拦截所有请求 进行token认证
serverApi.all('/*',function(req,res,next){
    let token = req.query.token

    // 放行不需要token验证的路径
    let arr = ['/login']            //不需要验证的路径
    let url = req.url.split('?')[0]     //请求路径
    if(arr.includes(url)){
        console.log('login 放行')
        next()
        return
    }

    // 不存在token 
    if(!token){
        res.send({
            Code:-1,
            info:'token不存在'
        })
        return
    }

    // token验证 
    let verification = Token.decrypt(token).token
    if(!verification){
        res.send({
            Code:-2,
            info:'token验证失败'
        })

        return
    }

    next()
})

// 注册管理员路由模块
const as = require('./router/administrators.js')
serverApi.use(as)

serverApi.listen(8180, function () {
    console.log('启动成功 runing http://127.0.0.1:8180')
})